package cn.jhc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.Account;
import cn.jhc.bean.User;
import cn.jhc.dao.AccountDAO;
import cn.jhc.dao.DaoType;
import cn.jhc.factory.DAOFactory;

/**
 * Servlet implementation class AddAccountServlet
 */
@WebServlet("/addaccount.do")
public class AddAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		User user = (User) request.getSession().getAttribute("currentUser");
		int userid = user.getUserId();
		Double b = null;
		String balance = request.getParameter("balance");
		if(balance != null){
			b = Double.parseDouble(balance);
		}
		String creditcard = (String)request.getParameter("creditcard");		
		AccountDAO accountDAO = (AccountDAO)DAOFactory.getDao(DaoType.ACCOUNT);
		Account account = new Account(b,creditcard,userid);
		if(accountDAO.addAccount(account)){
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println("<script>window.alert('添加失败！');</script>");
		}
		response.sendRedirect("./showaccount.htm");
		
	}

}
